<?php
  require_once('../../export/Worksheet.php');
  require_once('../../export/Workbook.php');
  // koneksi ke mysql
  require_once('../../config/koneksi.php');
  require_once('../../config/fungsi_tgl_indo.php');
  
  // function untuk membuat header file excel
  function HeaderingExcel() {
	  $tgl = date("dmY");
      header("Content-type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=data_keluarga.xls" );
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
      header("Pragma: public");
      }

  // membuat header file excel dan nama filenya
  HeaderingExcel('Data Keluarga.xls');

  // membuat workbook baru
  $workbook = new Workbook("");

  $worksheet1 =& $workbook->add_worksheet('Data Keluarga');

  // setting format header tabel data
  $format =& $workbook->add_format();
  $format->set_align('vcenter');
  $format->set_align('center');
  $format->set_color('white');
  $format->set_bold();
  $format->set_italic();
  $format->set_pattern();
  $format->set_fg_color('green');

  // membuat header tabel dengan format
  $worksheet1->set_row(0, 15);
  $worksheet1->set_column(0, 0, 5);
  $worksheet1->write_string(0, 0, "NO", $format);
  $worksheet1->set_column(0, 1, 20);
  $worksheet1->write_string(0, 1, "ID KK", $format);
  $worksheet1->set_column(0, 2, 50);
  $worksheet1->write_string(0, 2, "ALAMAT", $format);
  $worksheet1->set_column(0, 3, 5);
  $worksheet1->write_string(0, 3, "RT", $format);
  $worksheet1->set_column(0, 4, 5);
  $worksheet1->write_string(0, 4, "RW", $format);
  $worksheet1->set_column(0, 5, 15);
  $worksheet1->write_string(0, 5, "KODE DESA", $format);
  $worksheet1->set_column(0, 6, 25);
  $worksheet1->write_string(0, 6, "DESA", $format);
  $worksheet1->set_column(0, 7, 25);
  $worksheet1->write_string(0, 7, "KECAMATAN", $format);
  $worksheet1->set_column(0, 8, 25);
  $worksheet1->write_string(0, 8, "KABUPATEN", $format);
  $worksheet1->set_column(0, 9, 25);
  $worksheet1->write_string(0, 9, "PROVINSI", $format);
  $worksheet1->set_column(0, 10, 15);
  $worksheet1->write_string(0, 10, "DIKELUARKAN", $format);

  $query = "SELECT * FROM keluarga k inner join desa d on d.id_desa = k.id_desa inner join kecamatan kc on d.id_kecamatan = kc.id_kecamatan inner join kabupaten kb on kc.id_kabupaten = kb.id_kabupaten inner join propinsi p on kb.id_propinsi = p.id_propinsi";
  $hasil = mysql_query($query);
  $baris = 1;
  while ($data = mysql_fetch_array($hasil))
  {
		$no++;
        $worksheet1->write_string($baris, 0, $no);
        $worksheet1->write_string($baris, 1, $data['id_kk']);
        $worksheet1->write_string($baris, 2, $data['alamat']);
		$worksheet1->write_string($baris, 3, $data['rt']);
		$worksheet1->write_string($baris, 4, $data['rw']);
		$worksheet1->write_string($baris, 5, $data['id_desa']);
		$worksheet1->write_string($baris, 6, $data['desa']);
		$worksheet1->write_string($baris, 7, $data['kecamatan']);
		$worksheet1->write_string($baris, 8, $data['kabupaten']);
		$worksheet1->write_string($baris, 9, $data['propinsi']);
		$worksheet1->write_string($baris, 10, tgl_ind1($data['tanggal_dikeluarkan']));
        $baris++;
  }

  $workbook->close();
?>